// pages/counselor/qingjia/qingjia.js
Page({
  data: {
    classList: [
      { id: 0, name: '全部班级' },
      { id: 1, name: '2023级计算机科学1班' },
      { id: 2, name: '2023级软件工程1班' },
      { id: 3, name: '2022级数据科学1班' }
    ],
    classIndex: 0,
    statusList: ['全部', '待审批', '已批准', '已拒绝'],
    statusIndex: 0,
    leaveList: [],
    leaveStats: {
      total: 0,
      pending: 0,
      approved: 0,
      rejected: 0
    },
    isLoading: false
  },

  onLoad: function() {
    this.fetchLeaveList();
  },
  
  onShow: function() {
    // 页面显示时刷新数据
    this.fetchLeaveList();
  },
  
  // 班级变更
  classChange: function(e) {
    this.setData({
      classIndex: e.detail.value
    });
    
    this.fetchLeaveList();
  },
  
  // 状态变更
  statusChange: function(e) {
    this.setData({
      statusIndex: e.detail.value
    });
    
    this.fetchLeaveList();
  },
  
  // 获取请假列表
  fetchLeaveList: function() {
    this.setData({ isLoading: true });
    
    const classId = this.data.classList[this.data.classIndex].id;
    const status = this.data.statusList[this.data.statusIndex];
    
    wx.request({
      url: 'http://localhost:3000/api/counselor/leaves',
      method: 'GET',
      data: {
        classId: classId === 0 ? '' : classId,
        status: status === '全部' ? '' : status
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        if (res.statusCode === 200) {
          this.setData({
            leaveList: res.data.records,
            leaveStats: res.data.stats
          });
        }
      },
      fail: () => {
        // 使用模拟数据
        const mockLeaves = [
          { 
            id: 1, 
            studentName: '张三', 
            studentId: '2023001', 
            type: '事假', 
            startTime: '2023-09-15', 
            endTime: '2023-09-16', 
            days: 2, 
            reason: '家中有事', 
            status: '待审批',
            applyTime: '2023-09-14 10:25'
          },
          { 
            id: 2, 
            studentName: '李四', 
            studentId: '2023002', 
            type: '病假', 
            startTime: '2023-09-16', 
            endTime: '2023-09-18', 
            days: 3, 
            reason: '感冒发烧', 
            status: '已批准',
            applyTime: '2023-09-15 08:30',
            approver: '王老师'
          },
          { 
            id: 3, 
            studentName: '王五', 
            studentId: '2023003', 
            type: '事假', 
            startTime: '2023-09-17', 
            endTime: '2023-09-17', 
            days: 1, 
            reason: '办理银行卡', 
            status: '已拒绝',
            applyTime: '2023-09-16 15:40',
            approver: '王老师'
          },
          { 
            id: 4, 
            studentName: '赵六', 
            studentId: '2023004', 
            type: '病假', 
            startTime: '2023-09-18', 
            endTime: '2023-09-20', 
            days: 3, 
            reason: '发烧住院', 
            status: '待审批',
            applyTime: '2023-09-17 09:15'
          }
        ];
        
        // 根据筛选条件过滤
        let filteredLeaves = [...mockLeaves];
        
        if (this.data.classIndex !== 0) {
          // 模拟班级筛选，实际应该根据班级ID筛选
          if (this.data.classIndex === 1) {
            filteredLeaves = filteredLeaves.filter(item => ['2023001', '2023002'].includes(item.studentId));
          } else if (this.data.classIndex === 2) {
            filteredLeaves = filteredLeaves.filter(item => ['2023003', '2023004'].includes(item.studentId));
          } else if (this.data.classIndex === 3) {
            filteredLeaves = filteredLeaves.filter(item => item.studentId.startsWith('2022'));
          }
        }
        
        if (this.data.statusIndex !== 0) {
          const status = this.data.statusList[this.data.statusIndex];
          filteredLeaves = filteredLeaves.filter(item => item.status === status);
        }
        
        // 计算统计数据
        const stats = {
          total: filteredLeaves.length,
          pending: filteredLeaves.filter(item => item.status === '待审批').length,
          approved: filteredLeaves.filter(item => item.status === '已批准').length,
          rejected: filteredLeaves.filter(item => item.status === '已拒绝').length
        };
        
        this.setData({
          leaveList: filteredLeaves,
          leaveStats: stats
        });
      },
      complete: () => {
        this.setData({ isLoading: false });
        wx.stopPullDownRefresh();
      }
    });
  },
  
  // 查看请假详情
  viewLeaveDetail: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/qingjiaxiangqing/qingjiaxiangqing?id=${id}`
    });
  },
  
  // 导出请假记录
  exportLeaveRecords: function() {
    wx.showLoading({
      title: '导出中...',
    });
    
    const classId = this.data.classList[this.data.classIndex].id;
    const status = this.data.statusList[this.data.statusIndex];
    
    wx.request({
      url: 'http://localhost:3000/api/counselor/leaves/export',
      method: 'POST',
      data: {
        classId: classId === 0 ? '' : classId,
        status: status === '全部' ? '' : status
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        wx.hideLoading();
        
        if (res.statusCode === 200) {
          wx.showModal({
            title: '导出成功',
            content: '请假记录已导出，请到系统后台下载',
            showCancel: false
          });
        } else {
          wx.showToast({
            title: res.data.message || '导出失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.hideLoading();
        
        // 模拟成功
        wx.showModal({
          title: '导出成功',
          content: '请假记录已导出，请到系统后台下载',
          showCancel: false
        });
      }
    });
  },
  
  // 下拉刷新
  onPullDownRefresh: function() {
    this.fetchLeaveList();
  }
})